package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjProductCustomParam;
import com.zb.entity.ZjProductParamFieldVal;
import com.zb.mapper.ZjProductCustomParamMapper;
import com.zb.service.ZjProductCustomParamService;
import com.zb.vo.GoodsCustomParamVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品自定义参数关联表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-15
 */
@Service
public class ZjProductCustomParamServiceImpl extends ServiceImpl<ZjProductCustomParamMapper, ZjProductCustomParam> implements ZjProductCustomParamService {

    // 新增、修改、删除自定义参数
    @Override
    public void save(Long custAccId, Long productId, List<GoodsCustomParamVo> customParamVos, List<Long> delIdList) {
        if (null != delIdList || !delIdList.isEmpty()) {
            this.removeByIds(delIdList);
        }
        if (null == customParamVos || customParamVos.isEmpty()) {
            return;
        }
        Assert.isTrue(customParamVos.stream().map(GoodsCustomParamVo::getName).toList().size() == customParamVos.stream().map(GoodsCustomParamVo::getName).collect(Collectors.toSet()).size(),
                "产品自定义参数名称已重复！");
        List<ZjProductCustomParam> adds = new ArrayList<>();
        List<ZjProductCustomParam> updates = new ArrayList<>();
        ZjProductCustomParam param = null;
        for (GoodsCustomParamVo vo : customParamVos) {
            param = new ZjProductCustomParam();
            BeanUtils.copyProperties(vo, param);
            if (null != param.getId()) {
                updates.add(param);
                continue;
            }
            param.setId(IdWorker.getId());
            param.setCustAccId(custAccId);
            param.setProductId(productId);
            adds.add(param);
        }
        if (!adds.isEmpty()) {
            this.saveBatch(adds);
        }
        if (!updates.isEmpty()) {
            this.updateBatchById(updates);
        }
    }

    // 根据产品ID获取阁自定义参数
    @Override
    public List<ZjProductCustomParam> getByProductId(Long productId) {
        QueryWrapper<ZjProductCustomParam> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "val");
        wrapper.eq("product_id", productId);
        return this.list(wrapper);
    }

    // 根据产品ID删除产品自定义参数
    @Override
    public void deleteBatchByProductId(List<Long> productIds) {
        if (null == productIds || productIds.isEmpty()) {
            return;
        }
        QueryWrapper<ZjProductCustomParam> wrapper = new QueryWrapper<>();
        wrapper.in("product_id", productIds);
        this.remove(wrapper);
    }
}
